<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller{
    protected $sessionInfo;
    protected $store_ids;

    public function _initialize()
    {
        if(!$_SESSION['seller']){
            $this->redirect('Login/index');
        }
        $this->store_ids = $_SESSION['seller']['user_id'];
        $this->sessionInfo = $_SESSION['seller'];

        $this->assign('sessionInfo',$this->sessionInfo);
    }
    //分页
    protected function getPage($count,$page,$pageSize,$where=array()){
        $Page       = new \Think\Page($count,$pageSize);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('header','条留言');
        $Page->setConfig('first','首页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','尾页');
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%<p id="pageTips">第 '.$page.' 页/共 %TOTAL_PAGE% 页 ( '.$pageSize.' 条/页 共 %TOTAL_ROW% 条信息)</p>');
        foreach($where as $key=>$val) {
            $Page->parameter[$key]   =   $val;
        }
        $show       = $Page->show();// 分页显示输出
        return $show;
    }

    function abslength($s){
        $n = 0;
        preg_match_all("/./us",$s,$matchs);
        foreach($matchs[0] as $p){
            $n += preg_match('#^['.chr(0x1).'-'.chr(0xff).']$#',$p) ? 1 : 2;
        }
        return $n;
    }

    /**
     * 检查手机号码格式
     * @param $mobile 手机号码
     * @return bool;
     */
    protected function check_mobile($mobile){
        if(preg_match('/1\d{10}$/',$mobile))
            return true;
        return false;
    }

    function set_encrypt($data, $key = 'asa')
    {
        $key    =    md5($key);
        $x        =    0;
        $len    =    strlen($data);
        $l        =    strlen($key);
        for ($i = 0; $i < $len; $i++)
        {
            if ($x == $l)
            {
                $x = 0;
            }
            $char .= $key{$x};
            $x++;
        }
        for ($i = 0; $i < $len; $i++)
        {
            $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
        }
        return base64_encode($str);
    }
    /**
     * 检查邮箱地址格式
     * @param $email 邮箱地址
     * @return bool;
     */
    protected function check_email($email){
        if(filter_var($email,FILTER_VALIDATE_EMAIL))
            return true;
        return false;
    }

    public function create_png($text, $bool = false, $outfile = false, $level = 'H', $size = 10, $margin = 6, $saveandprint=false,$logopath,$store_name=null,$real_name=null)
    {
        require_once '../promotion/simplewind/Core/Library/Vendor/phpqrcode/phpqrcode.php';
        $qrcode = new \QRcode();
        if ( $outfile ) {
            if (!file_exists(dirname($outfile))){
                mkdir(dirname($outfile),0777,true);
            }
            $qrcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
            $QR = file_get_contents($outfile);
        } else {
            ob_start();
            $qrcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
            $QR = ob_get_contents();
            ob_end_clean();
        }

        //判断是否生成带logo的二维码
        if($bool)
        {
            $font = "/data/wwwroot/default/ecmall/promotion/Public/font/msyh.ttc";
            //字体大小
            $size = 15;
            //显示的文字
            $string = $store_name;
            $num1 = $this->abslength($string)/2;
            $x1 = 301-$num1;
            $logo = imagecreate(600,100);
            $black = ImageColorAllocate($logo, 255, 255, 255);
            $black1 = ImageColorAllocate($logo, 0, 0, 0);

            imagefilledrectangle($logo,0,0,600,60,$black);
            $info = imagettftext($logo, $size, 0, $x1, 28, $black1, $font, $string);
//
            if($real_name){
                $num2 = $this->abslength($real_name)/2;
                $x2 = 301-$num2;
                $info = imagettftext($logo, $size, 0, $x2, 50, $black1, $font, $real_name);
            }

            $QR = imagecreatefromstring(file_get_contents($outfile));    //目标图象连接资源。
            $QR_width = imagesx($QR);      //二维码图片宽度
            $QR_height = imagesy($QR);     //二维码图片高度
            $logo_width = imagesx($logo);    //logo图片宽度
            $logo_height = imagesy($logo);   //logo图片高度


            $logo_qr_width = $QR_width;   //组合之后logo的宽度(占二维码的1/5)
            $scale = $logo_width / $logo_qr_width;  //logo的宽度缩放比(本身宽度/组合后的宽度)
            $logo_qr_height = $logo_height / $scale; //组合之后logo的高度
            $from_width = ($QR_width - $logo_qr_width) / 2;  //组合之后logo左上角所在坐标点
            imagecopyresampled($QR, $logo, 0, 600, 0, 0, 580, 100, 600, 100);

            //输出图片
            imagepng($QR, $outfile);
            imagedestroy($QR);
            imagedestroy($logo);
        }
        return $outfile;
    }
    /**
     * @param string $name  字段名
     * @param string $val   字段值
     * @param string $msg   返回的错误信息
     * @param int $type     验证类型 : 1 是否为空 ，2 范围 , 3 手机号 ,4 邮箱
     * @param array $rage   type 为 2 的时候传 最小和最大值
     * @return bool
     * 欢迎扩展
     */
    public function validate($name='id',$val='',$msg='',$type=1, $rage=array(0,100)){

        if($type == 1){
            if(!$val){
                $this->error($msg?$msg:'缺少必传参数'.$name);
            }
        }else{
            if(!$val){
                $this->error($msg?$msg:'缺少必传参数'.$name);
            }
            switch ($type){
                case 2:    // 范围
                    if($val < $rage[0] || $val > $rage[1] ){
                        $this->error('参数值不在范围之内');
                    }
                    break;
                case 3:  // 手机号
                    if(!$this->check_mobile($val)){
                        $this->error('手机号码不正确');
                    }
                    break;
                case 4:  // email
                    if(!$this->check_email($val)){
                        $this->error('邮箱地址不正确');
                    }
                    break;
                case 5:
                    if(!is_numeric($val) || $val < $rage[0]){
                        $this->error('参数'.$name.'格式错误');
                    }
                    break;
                default:
                    $this->error('未检测到验证类型');
                    break;
            }
        }
        return true;
    }
}